﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin.SYS.PAFee
{
    public partial class Form : FormPage
    {
        BLL.PAFee bll = new BLL.PAFee();
        Model.PAFee model = new Model.PAFee();
        //
        int StoreID = 0, SupplierID = 0;
        BLL.Store bllStore = new BLL.Store();
        Model.Store modelStore = new Model.Store();
        BLL.Supplier bllSupplier = new BLL.Supplier();
        Model.Supplier modelSupplier = new Model.Supplier();
        //
        string DateFormat = "yyyy 年 MM 月 dd 日";

        protected void Page_Load(object sender, EventArgs e)
        {
            StoreID = DataTypePlus.requestInt(Request.QueryString["StoreID"]);
            SupplierID = DataTypePlus.requestInt(Request.QueryString["SupplierID"]);
            if (StoreID>0) modelStore = bllStore.GetModel(StoreID);
            else if (SupplierID > 0) modelSupplier = bllSupplier.GetModel(SupplierID);
            else { Master.ScriptManager_js(Master.ScriptManager_Alert_float_Str("参数错误", false) + Master.ScriptManager_close_adminRight_Str()); }
        }
        /// <summary>
        /// 初始化本页Model
        /// </summary>
        public override void Page_Load_DefaultModel() { }
        /// <summary>
        /// 初始化表单
        /// </summary>
        public override void Form_Default()
        {
            if (StoreID > 0) lb_EndDate.Text = Convert.ToDateTime(modelStore.PAFeeEndDate).ToString(DateFormat); 
            else lb_EndDate.Text =Convert.ToDateTime( modelSupplier.PAFeeEndDate).ToString(DateFormat);
            
        }
        /// <summary>
        /// 绑定表单数据【修改时用】
        /// </summary>
        public override void Form_Bind()
        {
            
        }
        /// <summary>
        /// 提交
        /// </summary>
        public override void Form_Submit()
        {
            decimal Money = DataTypePlus.requestDecimal(tb_Money.Text);
            DateTime EndDate = DateTime.ParseExact(tb_EndDate.Text, DateFormat, System.Globalization.CultureInfo.InvariantCulture);
            if (Master.noErr(Money == 0, "金额必须填写"))
            {
                model.StoreID = StoreID;
                model.SupplierID = SupplierID;
                model.Money = Money;
                model.EndDate = EndDate;
                Master.id = bll.Add(model);
                string operateCon = StoreID > 0 ? "孕婴店" + modelStore.ShortName : "供货商" + modelSupplier.ShortName + "缴纳联盟费" + StringPlus.showMoney(Money) + "到" + EndDate.ToShortDateString();
                BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.PAFee, Master.id, EnumHelper.OperateType.Add, operateCon);
                //
                if (StoreID > 0)
                {
                    modelStore.PAFeeEndDate = EndDate;
                    bllStore.Update(modelStore);
                    BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Store, StoreID, EnumHelper.OperateType.Update, operateCon);
                }
                else
                {
                    modelSupplier.PAFeeEndDate = EndDate;
                    bllSupplier.Update(modelSupplier);
                    BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Store, StoreID, EnumHelper.OperateType.Update, operateCon);
                }

                //通知孕婴店
                BLL.Message bllMessage = new BLL.Message();
                bllMessage.NewMessage(0, Security.GetCurrentUserID(), modelStore.UserID,
                    "您交纳的联盟服务费用共计" + StringPlus.showMoney(Money) + "元，我们将为您服务至" + EndDate.ToString("yyyy年MM月dd日") + "，感谢您的支持！",
                    true, true);

                Master.SubmitOK();
            }

        }
    }
}